/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvAdministratifvehicule;
import fr.cggveritas.crewintranet.jms.entities.CvAffectationbesoin;
import fr.cggveritas.crewintranet.jms.entities.CvTypemodel;
import fr.cggveritas.crewintranet.jms.entities.CvModel;
import fr.cggveritas.crewintranet.jms.entities.CvMarque;
import fr.cggveritas.crewintranet.jms.entities.CvEvaluationvehicule;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvVehiculeJpaController extends DaoImp implements Serializable {

    public CvVehiculeJpaController() {
        super();
    }

    public void create(CvVehicule cvVehicule) {
        if (cvVehicule.getCvEvaluationvehiculeList() == null) {
            cvVehicule.setCvEvaluationvehiculeList(new ArrayList<CvEvaluationvehicule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvAdministratifvehicule cvAdministratifvehicule = cvVehicule.getCvAdministratifvehicule();
            if (cvAdministratifvehicule != null) {
                cvAdministratifvehicule = em.getReference(cvAdministratifvehicule.getClass(), cvAdministratifvehicule.getIdvehicule());
                cvVehicule.setCvAdministratifvehicule(cvAdministratifvehicule);
            }
            CvTypemodel typemodel = cvVehicule.getTypemodel();
            if (typemodel != null) {
                typemodel = em.getReference(typemodel.getClass(), typemodel.getIdcvTypemodel());
                cvVehicule.setTypemodel(typemodel);
            }
            CvModel cvModelIdmodel = cvVehicule.getCvModelIdmodel();
            if (cvModelIdmodel != null) {
                cvModelIdmodel = em.getReference(cvModelIdmodel.getClass(), cvModelIdmodel.getIdmodel());
                cvVehicule.setCvModelIdmodel(cvModelIdmodel);
            }
            CvMarque cvMarqueIdmarque = cvVehicule.getCvMarqueIdmarque();
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque = em.getReference(cvMarqueIdmarque.getClass(), cvMarqueIdmarque.getIdmarque());
                cvVehicule.setCvMarqueIdmarque(cvMarqueIdmarque);
            }
            List<CvEvaluationvehicule> attachedCvEvaluationvehiculeList = new ArrayList<CvEvaluationvehicule>();
            for (CvEvaluationvehicule cvEvaluationvehiculeListCvEvaluationvehiculeToAttach : cvVehicule.getCvEvaluationvehiculeList()) {
                cvEvaluationvehiculeListCvEvaluationvehiculeToAttach = em.getReference(cvEvaluationvehiculeListCvEvaluationvehiculeToAttach.getClass(), cvEvaluationvehiculeListCvEvaluationvehiculeToAttach.getIdevaluationvehicule());
                attachedCvEvaluationvehiculeList.add(cvEvaluationvehiculeListCvEvaluationvehiculeToAttach);
            }
            cvVehicule.setCvEvaluationvehiculeList(attachedCvEvaluationvehiculeList);
            em.persist(cvVehicule);
            if (cvAdministratifvehicule != null) {
                CvVehicule oldCvVehiculeOfCvAdministratifvehicule = cvAdministratifvehicule.getCvVehicule();
                if (oldCvVehiculeOfCvAdministratifvehicule != null) {
                    oldCvVehiculeOfCvAdministratifvehicule.setCvAdministratifvehicule(null);
                    oldCvVehiculeOfCvAdministratifvehicule = em.merge(oldCvVehiculeOfCvAdministratifvehicule);
                }
                cvAdministratifvehicule.setCvVehicule(cvVehicule);
                cvAdministratifvehicule = em.merge(cvAdministratifvehicule);
            }
            if (typemodel != null) {
                typemodel.getCvVehiculeList().add(cvVehicule);
                typemodel = em.merge(typemodel);
            }
            if (cvModelIdmodel != null) {
                cvModelIdmodel.getCvVehiculeList().add(cvVehicule);
                cvModelIdmodel = em.merge(cvModelIdmodel);
            }
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque.getCvVehiculeList().add(cvVehicule);
                cvMarqueIdmarque = em.merge(cvMarqueIdmarque);
            }
            for (CvEvaluationvehicule cvEvaluationvehiculeListCvEvaluationvehicule : cvVehicule.getCvEvaluationvehiculeList()) {
                CvVehicule oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListCvEvaluationvehicule = cvEvaluationvehiculeListCvEvaluationvehicule.getCvVehiculeIdvehicule();
                cvEvaluationvehiculeListCvEvaluationvehicule.setCvVehiculeIdvehicule(cvVehicule);
                cvEvaluationvehiculeListCvEvaluationvehicule = em.merge(cvEvaluationvehiculeListCvEvaluationvehicule);
                if (oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListCvEvaluationvehicule != null) {
                    oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListCvEvaluationvehicule.getCvEvaluationvehiculeList().remove(cvEvaluationvehiculeListCvEvaluationvehicule);
                    oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListCvEvaluationvehicule = em.merge(oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListCvEvaluationvehicule);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvVehicule cvVehicule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvVehicule persistentCvVehicule = em.find(CvVehicule.class, cvVehicule.getIdvehicule());
            CvAdministratifvehicule cvAdministratifvehiculeOld = persistentCvVehicule.getCvAdministratifvehicule();
            CvAdministratifvehicule cvAdministratifvehiculeNew = cvVehicule.getCvAdministratifvehicule();
            CvTypemodel typemodelOld = persistentCvVehicule.getTypemodel();
            CvTypemodel typemodelNew = cvVehicule.getTypemodel();
            CvModel cvModelIdmodelOld = persistentCvVehicule.getCvModelIdmodel();
            CvModel cvModelIdmodelNew = cvVehicule.getCvModelIdmodel();
            CvMarque cvMarqueIdmarqueOld = persistentCvVehicule.getCvMarqueIdmarque();
            CvMarque cvMarqueIdmarqueNew = cvVehicule.getCvMarqueIdmarque();
            List<CvEvaluationvehicule> cvEvaluationvehiculeListOld = persistentCvVehicule.getCvEvaluationvehiculeList();
            List<CvEvaluationvehicule> cvEvaluationvehiculeListNew = cvVehicule.getCvEvaluationvehiculeList();
            List<String> illegalOrphanMessages = null;
            if (cvAdministratifvehiculeOld != null && !cvAdministratifvehiculeOld.equals(cvAdministratifvehiculeNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain CvAdministratifvehicule " + cvAdministratifvehiculeOld + " since its cvVehicule field is not nullable.");
            }
            for (CvEvaluationvehicule cvEvaluationvehiculeListOldCvEvaluationvehicule : cvEvaluationvehiculeListOld) {
                if (!cvEvaluationvehiculeListNew.contains(cvEvaluationvehiculeListOldCvEvaluationvehicule)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvEvaluationvehicule " + cvEvaluationvehiculeListOldCvEvaluationvehicule + " since its cvVehiculeIdvehicule field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cvAdministratifvehiculeNew != null) {
                cvAdministratifvehiculeNew = em.getReference(cvAdministratifvehiculeNew.getClass(), cvAdministratifvehiculeNew.getIdvehicule());
                cvVehicule.setCvAdministratifvehicule(cvAdministratifvehiculeNew);
            }
            if (typemodelNew != null) {
                typemodelNew = em.getReference(typemodelNew.getClass(), typemodelNew.getIdcvTypemodel());
                cvVehicule.setTypemodel(typemodelNew);
            }
            if (cvModelIdmodelNew != null) {
                cvModelIdmodelNew = em.getReference(cvModelIdmodelNew.getClass(), cvModelIdmodelNew.getIdmodel());
                cvVehicule.setCvModelIdmodel(cvModelIdmodelNew);
            }
            if (cvMarqueIdmarqueNew != null) {
                cvMarqueIdmarqueNew = em.getReference(cvMarqueIdmarqueNew.getClass(), cvMarqueIdmarqueNew.getIdmarque());
                cvVehicule.setCvMarqueIdmarque(cvMarqueIdmarqueNew);
            }
            List<CvEvaluationvehicule> attachedCvEvaluationvehiculeListNew = new ArrayList<CvEvaluationvehicule>();
            for (CvEvaluationvehicule cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach : cvEvaluationvehiculeListNew) {
                cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach = em.getReference(cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach.getClass(), cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach.getIdevaluationvehicule());
                attachedCvEvaluationvehiculeListNew.add(cvEvaluationvehiculeListNewCvEvaluationvehiculeToAttach);
            }
            cvEvaluationvehiculeListNew = attachedCvEvaluationvehiculeListNew;
            cvVehicule.setCvEvaluationvehiculeList(cvEvaluationvehiculeListNew);
            cvVehicule = em.merge(cvVehicule);
            if (cvAdministratifvehiculeNew != null && !cvAdministratifvehiculeNew.equals(cvAdministratifvehiculeOld)) {
                CvVehicule oldCvVehiculeOfCvAdministratifvehicule = cvAdministratifvehiculeNew.getCvVehicule();
                if (oldCvVehiculeOfCvAdministratifvehicule != null) {
                    oldCvVehiculeOfCvAdministratifvehicule.setCvAdministratifvehicule(null);
                    oldCvVehiculeOfCvAdministratifvehicule = em.merge(oldCvVehiculeOfCvAdministratifvehicule);
                }
                cvAdministratifvehiculeNew.setCvVehicule(cvVehicule);
                cvAdministratifvehiculeNew = em.merge(cvAdministratifvehiculeNew);
            }
            if (typemodelOld != null && !typemodelOld.equals(typemodelNew)) {
                typemodelOld.getCvVehiculeList().remove(cvVehicule);
                typemodelOld = em.merge(typemodelOld);
            }
            if (typemodelNew != null && !typemodelNew.equals(typemodelOld)) {
                typemodelNew.getCvVehiculeList().add(cvVehicule);
                typemodelNew = em.merge(typemodelNew);
            }
            if (cvModelIdmodelOld != null && !cvModelIdmodelOld.equals(cvModelIdmodelNew)) {
                cvModelIdmodelOld.getCvVehiculeList().remove(cvVehicule);
                cvModelIdmodelOld = em.merge(cvModelIdmodelOld);
            }
            if (cvModelIdmodelNew != null && !cvModelIdmodelNew.equals(cvModelIdmodelOld)) {
                cvModelIdmodelNew.getCvVehiculeList().add(cvVehicule);
                cvModelIdmodelNew = em.merge(cvModelIdmodelNew);
            }
            if (cvMarqueIdmarqueOld != null && !cvMarqueIdmarqueOld.equals(cvMarqueIdmarqueNew)) {
                cvMarqueIdmarqueOld.getCvVehiculeList().remove(cvVehicule);
                cvMarqueIdmarqueOld = em.merge(cvMarqueIdmarqueOld);
            }
            if (cvMarqueIdmarqueNew != null && !cvMarqueIdmarqueNew.equals(cvMarqueIdmarqueOld)) {
                cvMarqueIdmarqueNew.getCvVehiculeList().add(cvVehicule);
                cvMarqueIdmarqueNew = em.merge(cvMarqueIdmarqueNew);
            }
            for (CvEvaluationvehicule cvEvaluationvehiculeListNewCvEvaluationvehicule : cvEvaluationvehiculeListNew) {
                if (!cvEvaluationvehiculeListOld.contains(cvEvaluationvehiculeListNewCvEvaluationvehicule)) {
                    CvVehicule oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule = cvEvaluationvehiculeListNewCvEvaluationvehicule.getCvVehiculeIdvehicule();
                    cvEvaluationvehiculeListNewCvEvaluationvehicule.setCvVehiculeIdvehicule(cvVehicule);
                    cvEvaluationvehiculeListNewCvEvaluationvehicule = em.merge(cvEvaluationvehiculeListNewCvEvaluationvehicule);
                    if (oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule != null && !oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule.equals(cvVehicule)) {
                        oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule.getCvEvaluationvehiculeList().remove(cvEvaluationvehiculeListNewCvEvaluationvehicule);
                        oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule = em.merge(oldCvVehiculeIdvehiculeOfCvEvaluationvehiculeListNewCvEvaluationvehicule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvVehicule.getIdvehicule();
                if (findCvVehicule(id) == null) {
                    throw new NonexistentEntityException("The cvVehicule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvVehicule cvVehicule;
            try {
                cvVehicule = em.getReference(CvVehicule.class, id);
                cvVehicule.getIdvehicule();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvVehicule with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            CvAdministratifvehicule cvAdministratifvehiculeOrphanCheck = cvVehicule.getCvAdministratifvehicule();
            if (cvAdministratifvehiculeOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvVehicule (" + cvVehicule + ") cannot be destroyed since the CvAdministratifvehicule " + cvAdministratifvehiculeOrphanCheck + " in its cvAdministratifvehicule field has a non-nullable cvVehicule field.");
            }
            List<CvEvaluationvehicule> cvEvaluationvehiculeListOrphanCheck = cvVehicule.getCvEvaluationvehiculeList();
            for (CvEvaluationvehicule cvEvaluationvehiculeListOrphanCheckCvEvaluationvehicule : cvEvaluationvehiculeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvVehicule (" + cvVehicule + ") cannot be destroyed since the CvEvaluationvehicule " + cvEvaluationvehiculeListOrphanCheckCvEvaluationvehicule + " in its cvEvaluationvehiculeList field has a non-nullable cvVehiculeIdvehicule field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            CvTypemodel typemodel = cvVehicule.getTypemodel();
            if (typemodel != null) {
                typemodel.getCvVehiculeList().remove(cvVehicule);
                typemodel = em.merge(typemodel);
            }
            CvModel cvModelIdmodel = cvVehicule.getCvModelIdmodel();
            if (cvModelIdmodel != null) {
                cvModelIdmodel.getCvVehiculeList().remove(cvVehicule);
                cvModelIdmodel = em.merge(cvModelIdmodel);
            }
            CvMarque cvMarqueIdmarque = cvVehicule.getCvMarqueIdmarque();
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque.getCvVehiculeList().remove(cvVehicule);
                cvMarqueIdmarque = em.merge(cvMarqueIdmarque);
            }
            em.remove(cvVehicule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvVehicule> findCvVehiculeEntities() {
        return findCvVehiculeEntities(true, -1, -1);
    }

    public List<CvVehicule> findCvVehiculeEntities(int maxResults, int firstResult) {
        return findCvVehiculeEntities(false, maxResults, firstResult);
    }

    private List<CvVehicule> findCvVehiculeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvVehicule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvVehicule findCvVehicule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvVehicule.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvVehiculeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvVehicule> rt = cq.from(CvVehicule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    public List<CvVehicule> findCvChauffeursDisponible() {
        EntityManager em = getEntityManager();
        try {

            String queryString = "select cva from CvAffectationbesoin cva "
                    + "where cva.statue =:x";

            Query query = em.createQuery(queryString);
            Short statu = 0;
            query.setParameter("x", statu);

            List<CvAffectationbesoin> resul1 = query.getResultList();


            if (resul1 != null) {
                List<Integer> id = new ArrayList<Integer>();
                int i = 0;
                for (CvAffectationbesoin cva : resul1) {
                    id.add(cva.getIdvehicule().getIdvehicule());
                }

                String queryString1 = "select c from CvVehicule c ";
                Query query1 = em.createQuery(queryString1);

                List<CvVehicule> resul = query1.getResultList();
                List<CvVehicule> resul2 = new ArrayList<CvVehicule>();
                if (resul != null) {
                    for (CvVehicule cvt : resul) {
                        if (!id.contains(cvt.getIdvehicule())) {
                            resul2.add(cvt);
                        }
                    }
                    
                    return resul2;
                }
                return new ArrayList<CvVehicule>();
            }
            return new ArrayList<CvVehicule>();

        } finally {
            em.close();
        }
    }
}
